package com.bsc.relay.service;

import com.bsc.relay.server.command.Command;

import com.bsc.relay.server.command.MessageEvents;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientHandler extends IoHandlerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientHandler.class);

    public void sessionOpened(IoSession session) throws Exception {
    }

    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        if (!(message instanceof Command)) {
            LOGGER.warn("Unexpected message, drop it.");
        }
        Command command = (Command) message;
        if(command.getMessageEvents().equals(MessageEvents.MESSAGE_RESULT)){
            if(!command.isResult()){
                LOGGER.warn("send message unexpected!!");
            }
             LOGGER.info("result message!!");
        }else if(command.getMessageEvents().equals(MessageEvents.MESSAGE_SEND)){
            LOGGER.info("get message!!");
        }

        LOGGER.info("111: " + command.isResult() + " 22 " + command.getCommandName());


    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        LOGGER.warn(cause.getMessage(), cause);
    }
}
